package com.sz.app.oss.api.logistics.bill;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.bill.dto.BillingReGenerateDto;
import com.sz.common.base.dto.CommonModel;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import com.sz.biz.logistics.bill.entity.BillingTaskStatus;
import com.sz.biz.logistics.bill.service.BillingService;
import com.sz.biz.logistics.bill.service.BillingTaskService;
import com.sz.biz.logistics.bill.dto.BillingDetailDto;
import com.sz.biz.logistics.bill.dto.BillingDto;
import com.sz.biz.logistics.bill.dto.BillingTaskQueryDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: 账单任务管理 <br>
 * Author: jifeixing <br>
 * Date: 2017-05-17 14:18:00
 */
@RestController
@RequestMapping("${api}/biz/financial/task/{type}")
@Api(description = " ", tags = "041-001、账单任务管理")
public class BillingTaskController extends BaseController {

    @Autowired
    private BillingService billingService;
    @Autowired
    private BillingTaskService billingTaskService;

    /**
     * 确认生成账单信息
     * @param billingTaskQueryDto
     * @return
     */
    @ApiOperation(value = "确认生成账单信息", notes = "确认生成账单信息<br>权限："+ Permissions.BIZ_MGT_BASE_FINANCIAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "billingTaskQueryDto", value = "确认生成账单参数信息\r\n "
            + "customerIds(客户数组), \r\nstartEffectTime(yyyy-MM-dd HH:mm:ss), \r\nendEffectTime(yyyy-MM-dd HH:mm:ss)", required = true, dataType = "BillingTaskQueryDto", paramType = "body") })
    @RequestMapping(value = "/preGenerate", method = { RequestMethod.POST })
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FINANCIAL)
    public ResultDto preGenerateBill(@PathVariable String type,@RequestBody BillingTaskQueryDto billingTaskQueryDto) {
        billingTaskQueryDto.setType(type);
        BillingDto billingDtoReturn = billingTaskService.preGenerateBill(billingTaskQueryDto);
        ResultDto resultDto = new ResultDto();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("startEffectTime", billingDtoReturn.getStartEffectTime());
        resultMap.put("endEffectTime", billingDtoReturn.getEndEffectTime());
        resultMap.put("customerIds", billingDtoReturn.getCustomerIds());
        resultMap.put("orderCounts", billingDtoReturn.getOrderCounts());
        resultDto.setData(resultMap);
        return resultDto;
    }

    /**
     * 生成账单
     * @param billingTaskQueryDto
     * @return
     */
    @ApiOperation(value = "生成账单", notes = "生成账单<br>权限："+ Permissions.BIZ_MGT_BASE_FINANCIAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "billingTaskQueryDto", value = "生成账单参数信息\r\n "
            + "customerIds(客户数组/供应商数组), startEffectTime(yyyy-MM-dd HH:mm:ss), endEffectTime(yyyy-MM-dd HH:mm:ss)，type(1：采购订单，2：销售订单)", required = true, dataType = "BillingTaskQueryDto", paramType = "body") })
    @RequestMapping(value = "/generate", method = { RequestMethod.POST })
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FINANCIAL)
    public ResultDto generateBill(@PathVariable String type,@RequestBody BillingTaskQueryDto billingTaskQueryDto) {
        billingTaskQueryDto.setType(type);
        String taskCode = billingTaskService.generateBill(billingTaskQueryDto);
        return this.getTaskStartSuccessDto(taskCode);
    }

    /**
     * 根据任务编码删除任务
     * @param taskCodes
     * @return
     */
    @ApiOperation(value = "根据任务编码删除任务-只能删除已完成的任务", notes = "根据任务编码删除任务-只能删除已完成的任务<br>权限：" + Permissions.BIZ_MGT_BASE_FINANCIAL)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
                    @ApiImplicitParam(name = "taskCodes", value = "taskCodes", required = true, dataType = "List", paramType = "body")})
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FINANCIAL)
    public ResultDto batchDeleteBillingByNos(@PathVariable String type,@RequestBody List<String> taskCodes) {
        billingTaskService.batchdeleteByTaskCode(taskCodes,type);
        return getDeleteSuccessDto(MessagesCodes.MODULE_FINANCIAL_TASK);
    }

    public BillingService getBillingService() {
        return billingService;
    }

    public void setBillingService(BillingService billingService) {
        this.billingService = billingService;
    }

    /**
     * 重新生成账单
     * @param billingReGenerateDto
     * @return
     */
    @ApiOperation(value = "重新生成账单", notes = "重新生成账单<br>权限："+ Permissions.BIZ_MGT_BASE_FINANCIAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "billingReGenerateDto", value = "重试的任务编码列表", required = true, dataType = "BillingReGenerateDto", paramType = "body")
    })
    @RequestMapping(value = "/regenerate", method = { RequestMethod.POST })
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FINANCIAL)
    public ResultDto regenerateBill(@PathVariable String type, @RequestBody BillingReGenerateDto billingReGenerateDto) {
        billingTaskService.regenerate(billingReGenerateDto.getTaskCodes(),type,billingReGenerateDto.getCustomers());
        return this.getOperationSuccessDto(MessagesCodes.MODULE_FINANCIAL);
    }

    /**
     * 所有账单任务状态数据
     *
     * @return
     */
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @ApiOperation(value = "所有账单任务状态数据", notes = "所有账单任务状态数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path") })
    public ResultDto getAllBillingTaskStatus(@PathVariable String type) {
        List<CommonModel> list = billingTaskService.getAllTaskStatus();
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    /**
     * 分页查询账单任务列表
     */
    @ApiOperation(value = "账单任务列表", notes = "账单任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "taskCode", value = "任务编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "taskStatus", value = "任务状态", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始账单周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束账单周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "taskStartTime", value = "起始任务周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "taskEndTime", value = "结束任务周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findBillingTaskList(@PathVariable String type,
                                         @RequestParam(required = false) String q,
                                         @RequestParam(required = false) String taskCode,
                                         @RequestParam(required = false) BillingTaskStatus taskStatus,
                                         @RequestParam(required = false) String startEffectTime,
                                         @RequestParam(required = false) String endEffectTime,
                                         @RequestParam(required = false) String taskStartTime,
                                         @RequestParam(required = false) String taskEndTime,
                                         @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                         @RequestParam(required = false, defaultValue = "10") int pageSize,
                                         @RequestParam(required = false) String sort,
                                         @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        QueryParams params = new QueryParams(pageSize, pageIndex, q, sort, asc);
        params.put("startEffectTime", billingService.populateTime(startEffectTime));
        params.put("endEffectTime", billingService.populateTime(endEffectTime));
        params.put("taskStartTime", billingService.populateTime(taskStartTime));
        params.put("taskEndTime", billingService.populateTime(taskEndTime));
        params.put("taskCode", taskCode);
        params.put("taskStatus", taskStatus);
        params.put("type", type);
        return billingTaskService.findBillingTaskList(params);
    }

    /**
     * 账单任务详情
     */
    @ApiOperation(value = "账单任务详情", notes = "账单任务详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
        @ApiImplicitParam(name = "taskCode", value = "任务编码", required = true, dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/detail", method = {RequestMethod.GET})
    public ResultDto billingTaskDetail(@PathVariable String type,@RequestParam(required = false) String taskCode) {
        BillingDetailDto billingDetailDto = billingTaskService.billingTaskDetail(taskCode,type);
        ResultDto dto = new ResultDto();
        dto.setData(billingDetailDto);
        return dto;
    }
}
